package com.sgb.kjwl.viewmodel.self.carMakers;

import android.app.Application;
import android.content.Context;

import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.swgk.core.base.api.ICallBack;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.self.CarriagePushEntity;
import com.sgb.kjwl.model.response.ListResponseV1;
import com.sgb.net.util.RequestUtil;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;

import java.util.List;

import retrofit2.Call;

public class CarriageHistoryModel extends AndroidViewModel {
    public MutableLiveData<ListResponseV1<List<CarriagePushEntity>>> ldList = new MutableLiveData<>();
    private int mPage = 1;

    public CarriageHistoryModel(Application application) {
        super(application);
    }

    public void getList(Context context, boolean isRefresh) {
        if (isRefresh) {
            mPage = 1;
        }
        RequestUtil requestUtil = RequestUtil.map();
        requestUtil.add("status", true);
        requestUtil.add("page", mPage);
        requestUtil.add("limit", 10);
        DialogHelper.showProgressDialog(context, "请求中...");
        NewAPIManager.getInstance().getLogisticsAPI().getCarrierHistoryList(requestUtil.buildMap()).enqueue(new ICallBack<BaseEntity<ListResponseV1<List<CarriagePushEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponseV1<List<CarriagePushEntity>>> data) {
                if (!data.isSuccess()) {
                    ldList.postValue(new ListResponseV1<>(false));
                    return;
                }
                ldList.postValue(data.getData());
                if (data.getData().haveMoreData()) {
                    mPage = data.getData().nextPage();
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponseV1<List<CarriagePushEntity>>>> call, Throwable t) {
                super.onFailure(call, t);
                ldList.postValue(new ListResponseV1<>(false));
            }
        });
    }
}
